<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\logic;

use app\common\logic\Article as CommonArticle;

/**
 * 文章接口逻辑
 */
class Article extends ApiBase
{
    
    public static $commonArticleLogic = null;
    
    /**
     * 基类初始化
     */
    public function __construct()
    {
        // 执行父类构造方法
        parent::__construct();
        
        empty(static::$commonArticleLogic) && static::$commonArticleLogic = get_sington_object('Article', CommonArticle::class);
    }
    
    /**
     * 获取文章分类列表
     */
    public function getArticleCategoryList()
    {
        //$list=static::$commonArticleLogic->getArticleCategoryList([], 'id,name', 'id desc', false);
        $list=db('article_category')->where("id != 10 and id != 11 and status=1")->order( 'sort Asc')->select();
		foreach($list as $k=>$v){
			$lists=db('article')->field('id,name,category_id,describe,create_time,cover_id')->where(array('category_id'=>$v['id'],'status'=>1))->select();
			foreach($lists as $key=>$val){
				$lists[$key]['img']=get_picture_url($val['cover_id']);
				$lists[$key]['url']='http://'.$_SERVER['SERVER_NAME'].'/index.php/index/details/id/'.$val['id'];
			}
			$list[$k]['list']=$lists;
		}
        return $list;
    }
    /**
     * 获取文章列表
     */
    public function getArticleList($data = [])
    {
        $list_rows=!empty($data['list_rows'])?$data['list_rows']:10;
        $data['page']=!empty($data['page'])?$data['page']:1;
        $page=($data['page']-1)*$list_rows;
        $where = [];
		$where['status']=1;
        if(!empty($data['is']) && $data['is']==2){
            $where['category_id']=13;
        }else{
            $where['category_id']=10;
        }
        $list=db('article')->where($where)->order('id desc')->limit($page,$list_rows)->select();
        foreach($list as $key=>$val){
            $list[$key]['img']=get_picture_url($val['cover_id']);
            $list[$key]['url']='http://'.$_SERVER['SERVER_NAME'].'/index.php/index/details/id/'.$val['id'];
        }

        return $list;
    }
    
    /**
     * 获取文章信息
     */
    public function getArticleInfo($data = [])
    {
		
        return static::$commonArticleLogic->getArticleInfo(['a.id' => $data['id']], 'a.*,m.nickname,c.name as category_name');
    }
}
